package com.kfh.ybracelet.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kfh.ybracelet.R;
import com.kfh.ybracelet.base.BaseFragment;
import com.kfh.ybracelet.find.CustomerServiceActivity;
import com.kfh.ybracelet.find.DonateActivity;
import com.kfh.ybracelet.find.InquireActivity;
import com.kfh.ybracelet.my.AddBabyActivity;
import com.kfh.ybracelet.pay.PayActivity;
import com.kfh.ybracelet.sim.IdentityActivity;
import com.kfh.ybracelet.sim.NewSimActivity;
import com.kfh.ybracelet.sim.OrderListActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

/**
 * 发现模块
 * Created by Aaron on 16/2/29.
 */
public class FindFragment extends BaseFragment implements View.OnClickListener {

    private Intent mIntent;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View findView = inflater.inflate(R.layout.fragment_find, null);

        initWidget(findView);

        return findView;
    }

    /**
     * 初始化view
     *
     * @param findView
     */
    private void initWidget(View findView) {

        TextView tv_title = (TextView) findView.findViewById(R.id.tv_title);
        tv_title.setText(getResources().getString(R.string.find));

        RelativeLayout rldonate = (RelativeLayout) findView.findViewById(R.id.rl_donate);
        RelativeLayout rladdold = (RelativeLayout) findView.findViewById(R.id.rl_add_old);
//        RelativeLayout rlscan = (RelativeLayout) findView.findViewById(R.id.rl_scan);
        RelativeLayout rl_inquire = (RelativeLayout) findView.findViewById(R.id.rl_inquire);
//        RelativeLayout rl_paysim = (RelativeLayout) findView.findViewById(R.id.rl_paysim);
        RelativeLayout rl_identity = (RelativeLayout) findView.findViewById(R.id.rl_identity);
        RelativeLayout rl_customerService = (RelativeLayout) findView.findViewById(R.id.rl_customerService);
        RelativeLayout rl_recharge = (RelativeLayout) findView.findViewById(R.id.rl_recharge);
        RelativeLayout rl_sim = (RelativeLayout) findView.findViewById(R.id.rl_sim);
        RelativeLayout rl_list = (RelativeLayout) findView.findViewById(R.id.rl_list);

        rldonate.setOnClickListener(this);
        rladdold.setOnClickListener(this);
//        rlscan.setOnClickListener(this);
        rl_inquire.setOnClickListener(this);
//        rl_paysim.setOnClickListener(this);
        rl_identity.setOnClickListener(this);
        rl_customerService.setOnClickListener(this);
        rl_recharge.setOnClickListener(this);
        rl_sim.setOnClickListener(this);
        rl_list.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

//            case R.id.rl_scan://扫一扫
//
//                mIntent = new Intent(getActivity(), CaptureActivity.class);
//                startActivity(mIntent);
//
//                break;

            case R.id.rl_add_old://添加老人

                mIntent = new Intent(getActivity(), AddBabyActivity.class);
                startActivity(mIntent);

                break;

            case R.id.rl_donate://捐赠

                mIntent = new Intent(getActivity(), DonateActivity.class);
                startActivity(mIntent);

                break;

            case R.id.rl_inquire://问卷调查

                mIntent = new Intent(getActivity(), InquireActivity.class);
                startActivity(mIntent);

                break;

//            case R.id.rl_paysim://充值页面
//
//                mIntent = new Intent(getActivity(), PaySimActivity.class);
//                startActivity(mIntent);
//
//                break;

            case R.id.rl_identity://实名认证

                mIntent = new Intent(getActivity(), IdentityActivity.class);
                startActivity(mIntent);

                break;
            case R.id.rl_customerService://客户服务(Q&A)

                mIntent = new Intent(getActivity(), CustomerServiceActivity.class);
                startActivity(mIntent);

                break;

            case R.id.rl_recharge://充值缴费

//                charge();
                mIntent = new Intent(getActivity(), PayActivity.class);
                startActivity(mIntent);

                break;

            case R.id.rl_sim://新开套餐

                mIntent = new Intent(getActivity(), NewSimActivity.class);
                startActivity(mIntent);

                break;

            case R.id.rl_list://订单列表

                mIntent = new Intent(getActivity(), OrderListActivity.class);
                startActivity(mIntent);

                break;

        }

    }

//    private void charge() {
//        JSONObject j = new JSONObject();
//
//        j.put("token", sp.getString(Constant.TOKEN, ""));
//        j.put("out_trade_no", getOutTradeNo() + "");
//        j.put("subject", "标题测试");
//        j.put("body", "android测试");
//        j.put("total_amount", "0.01");
//
//        String params = j.toJSONString();
//
//        Log.e("tag", "params:" + params);
//        HttpUtil.setPostRequest("生成订单中...",getActivity(), "http://10.0.0.6:10008/yellow/alipay", params,
//                getClass().getName(), new HttpUtil.SuccessListener() {
//                    @Override
//                    public void setOnSuccessListener(final String str) {
//
//                        try {
//
//                            org.json.JSONObject result = new org.json.JSONObject(str);
//
//                            final String orderInfo = result.getString("sign");
//
//                            Log.e("tag", " orderInfo :" + orderInfo);
//
//                            Runnable payRunnable = new Runnable() {
//
//                                @Override
//                                public void run() {
//                                    PayTask alipay = new PayTask(getActivity());
//                                    Map<String, String> result = alipay.payV2(orderInfo, true);
//                                    Log.e("msp", result.toString());
//
//                                    Message msg = new Message();
//                                    msg.what = SDK_PAY_FLAG;
//                                    msg.obj = result;
//                                    mHandler.sendMessage(msg);
//                                }
//                            };
//
//                            Thread payThread = new Thread(payRunnable);
//                            payThread.start();
//
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
//
//
//
//
//                    }
//                }, new HttpUtil.FailListener() {
//                    @Override
//                    public void setOnFailListener() {
//
//                    }
//                });
//    }


    private static String getOutTradeNo() {
        SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss", Locale.getDefault());
        Date date = new Date();
        String key = format.format(date);

        Random r = new Random();
        key = key + r.nextInt();
        key = key.substring(0, 15);
        return key;
    }


//    private static final int SDK_PAY_FLAG = 1;
//    @SuppressLint("HandlerLeak")
//    private Handler mHandler = new Handler() {
//        @SuppressWarnings("unused")
//        public void handleMessage(Message msg) {
//            switch (msg.what) {
//                case SDK_PAY_FLAG: {
//                    @SuppressWarnings("unchecked")
//                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
//                    /**
//                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
//                     */
//                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
//                    String resultStatus = payResult.getResultStatus();
//                    // 判断resultStatus 为9000则代表支付成功
//                    if (TextUtils.equals(resultStatus, "9000")) {
//                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
//                        Toast.makeText(getActivity(), "支付成功", Toast.LENGTH_SHORT).show();
//                    } else {
//                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
//                        Toast.makeText(getActivity(), "支付失败", Toast.LENGTH_SHORT).show();
//                    }
//                    break;
//                }
//
//                default:
//                    break;
//            }
//        }
//
//        ;
//    };

}